package Connection;

import Controllers.Application;
import StateInformation.GameState;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class WebUserManager implements UserManager {

    private Application application;

    public WebUserManager(Application application) {
        this.application = application;
    }
    
    @Override
    public boolean register(String username, String password, String name, String surname, String email) {
        try {
            URLConnection connection = getConnection();
    
            OutputStream outputStream = connection.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject("register");
            objectOutputStream.writeObject(username);
            objectOutputStream.writeObject(password);
            objectOutputStream.writeObject(name);
            objectOutputStream.writeObject(surname);
            objectOutputStream.writeObject(email);
            objectOutputStream.flush();
            objectOutputStream.close();
    
            InputStream inputStream = connection.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            boolean registered = (boolean) objectInputStream.readObject();
            objectInputStream.close();

            return registered;
        } catch (Exception e) {
            try {
                File file = new File("C:/Users/Jonatan/Desktop/WebUserManager_register.txt");
                file.createNewFile();
                FileOutputStream fileOutpuStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(fileOutpuStream);
                objectOutputStream1.writeObject(e.getMessage());
                objectOutputStream1.flush();
                objectOutputStream1.close();
            } catch (IOException ex1) {
            }
        }
        return false;
    }

    @Override
    public boolean logIn(String name, String password) {
        return false;
    }

    @Override
    public void setGameState(String user, GameState gameState) {
    }

    @Override
    public GameState getGameState(String user) {
        return null;
    }

    public URLConnection getConnection() {
        IPServer iPServer = new IPServer(application);
        URLConnection urlConnection = null;
        String location = iPServer.getAdress() + "/ArkanoidServidor/UserManagerServlet";

        try {
            URL url = new URL(location);
            urlConnection = url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        } catch (Exception ex) {
            try {
                File file = new File("C:/Users/Jonatan/Desktop/WebUserManager_getConnection.txt");
                file.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(ex.getMessage());
                objectOutputStream.flush();
                objectOutputStream.close();
            } catch (IOException ex1) {
            }
        }
        return urlConnection;
    }
}
